package com.yanqu.road.dao.impl.cross.unionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.UnionWarUnionJoin;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionWarUnionJoinDaoImpl extends TempDataObjectDao<UnionWarUnionJoin> {


    public static String getUnionWarUnionJoinSql = "select * from t_u_activity_unionwar_union where activityId = ?";

    public Map<Integer, List<UnionWarUnionJoin>> getUnionWarUnionJoin(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<UnionWarUnionJoin>> dataListMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUnionWarUnionJoinSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionWarUnionJoin data = parse(rs);
                if(!dataListMap.containsKey(data.getGroupId())){
                    dataListMap.put(data.getGroupId(), new ArrayList<>());
                }
                dataListMap.get(data.getGroupId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataListMap;
    }


    public static String insertUnionWarUnionJoinSql = "insert into t_u_activity_unionwar_union(activityId,unionUid,rankNum,unionBaseInfo,pushSuccess,phaseOneScore,phaseOneRank,phaseTwoScore,phaseTwoRank,needSync,groupId) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UnionWarUnionJoin data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRankNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionBaseInfoJsonStr()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isPushSuccess()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPhaseOneScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhaseOneRank()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPhaseTwoScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhaseTwoRank()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isNeedSync()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertUnionWarUnionJoinSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionWarUnionJoinSql = "update t_u_activity_unionwar_union set rankNum=?,unionBaseInfo=?,pushSuccess=?,phaseOneScore=?,phaseOneRank=?,phaseTwoScore=?,phaseTwoRank=?,needSync=?,groupId=? where activityId=? and unionUid=?";

    @Override
    protected boolean updateData(UnionWarUnionJoin data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getRankNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionBaseInfoJsonStr()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isPushSuccess()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPhaseOneScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhaseOneRank()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPhaseTwoScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhaseTwoRank()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isNeedSync()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        if (getDb().executeNoneQuery(updateUnionWarUnionJoinSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionWarUnionJoin parseData(ResultSet rs) throws SQLException {
        UnionWarUnionJoin unionWarUnionJoin = new UnionWarUnionJoin();
        unionWarUnionJoin.setActivityId(rs.getInt("activityId"));
        unionWarUnionJoin.setUnionUid(rs.getString("unionUid"));
        unionWarUnionJoin.setRankNum(rs.getInt("rankNum"));

        String baseUserInfoStr = rs.getString("unionBaseInfo");
        if(!StringUtils.isNullOrEmpty(baseUserInfoStr)){
            UnionBaseInfo unionBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UnionBaseInfo>() {});
            if(null != unionBaseInfo) {
                unionWarUnionJoin.setUnionBaseInfo(unionBaseInfo);
            }
        }
        unionWarUnionJoin.setPushSuccess(rs.getBoolean("pushSuccess"));
        unionWarUnionJoin.setPhaseOneScore(rs.getLong("phaseOneScore"));
        unionWarUnionJoin.setPhaseOneRank(rs.getInt("phaseOneRank"));
        unionWarUnionJoin.setPhaseTwoScore(rs.getLong("phaseTwoScore"));
        unionWarUnionJoin.setPhaseTwoRank(rs.getInt("phaseTwoRank"));
        unionWarUnionJoin.setNeedSync(rs.getBoolean("needSync"));
        unionWarUnionJoin.setGroupId(rs.getInt("groupId"));
        return unionWarUnionJoin;
    }
}
